package com.emip.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.ItemBean;
import com.emip.bean.MonitorTypeBean;
import com.emip.bean.QualityTypeBean;
import com.emip.dao.QualityTypeDAO;
import com.emip.service.QualityTypeService;

@Service("qualityTypeService")
public class QualityTypeServiceImpl implements QualityTypeService {

	@Autowired
	QualityTypeDAO qualityTypeDAO;
	
	public QualityTypeBean addQualityType(MonitorTypeBean monitorType, String name,
			ItemBean item, double MaxValue, double MinValue, String remark, int qlevel) {
		QualityTypeBean newBean = new QualityTypeBean();
		newBean.setMonitorType(monitorType);
		newBean.setName(name);
		newBean.setItem(item);
		newBean.setMaxValue(MaxValue);
		newBean.setMinValue(MinValue);
		newBean.setRemark(remark);
		newBean.setQlevel(qlevel);
		//查重，是否已经有相同的name, monitorType, item的记录存在
		//若有重复记录，则返回null
		if (qualityTypeDAO.findExist(newBean)) {
			return null;
		}
		int newID = qualityTypeDAO.add(newBean);
		newBean.setId(newID);
		return newBean;
		
	}

	public Boolean deleteQualityType(int id) {
		QualityTypeBean delBean = qualityTypeDAO.findByID(id);
		qualityTypeDAO.remove(delBean);
		return true;
	}

	public List<QualityTypeBean> getAll() {
		return qualityTypeDAO.getAll();
	}

	public QualityTypeBean getQualityTypeByID(int id) {
		return qualityTypeDAO.findByID(id);
	}

	public QualityTypeBean updateQualityType(int id, MonitorTypeBean monitorType, String name,
			ItemBean item, double MaxValue, double MinValue, String remark, int qlevel) {
		QualityTypeBean newBean = qualityTypeDAO.findByID(id);
		newBean.setMonitorType(monitorType);
		newBean.setName(name);
		newBean.setItem(item);
		newBean.setMaxValue(MaxValue);
		newBean.setMinValue(MinValue);
		newBean.setRemark(remark);
		newBean.setQlevel(qlevel);
		//查重，是否已经有相同的name, monitorType, item的记录存在
		//若有重复记录，则返回null
		if (qualityTypeDAO.findExist(newBean)) {
			return null;
		}
		qualityTypeDAO.update(newBean);
		return newBean;
	}

}
